package com.tanhua.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.util.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.service.RecommendUserService;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Service
public class TodayBestServiceImpl implements TodayBestService {
    @Autowired
    private UserService userService;
    @Autowired
    private RecommendUserService recommendUserService;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public TodayBest queryTodayBest() {
        User user = UserThreadLocal.get();

        //recommendUserService只能去mongodb中查询出todayBest的id和fateValue
        TodayBest todayBest = recommendUserService.queryTodayBest(user.getId());

        //如果是首次使用探花交友app,没有历史查看记录,也就mongodb中也就没有推荐佳人;则可以随便给一个推荐
        if (null == todayBest) {
            todayBest = new TodayBest();
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", user.getId());
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);
            SexEnum sex = userInfo.getSex();

            if (sex.getValue() == 1) {//用户性别为男,则推荐女
                QueryWrapper<UserInfo> query = new QueryWrapper<>();
                query.eq("sex", 2).last("limit 1");
                UserInfo randomRecommend = userInfoMapper.selectOne(query);//随机推荐一个

                todayBest.setId(randomRecommend.getId());
            } else {
                QueryWrapper<UserInfo> query = new QueryWrapper<>();
                query.eq("sex", 1).last("limit 1");
                UserInfo randomRecommend = userInfoMapper.selectOne(query);//随机推荐一个
                todayBest.setId(randomRecommend.getId());
            }
        }

        // todayBest剩下的5个字段需要去mysql查询
        UserInfo userInfo = userInfoMapper.selectById(todayBest.getId());
        todayBest.setAvatar(userInfo.getCoverPic());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfo.getAge());
        todayBest.setTags(userInfo.getTags().split(","));

        return todayBest;
    }

    @Override
    public PageResult queryRecommendation( RecommendUserQueryParam recommendUserQueryParam) {
        User user = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(recommendUserQueryParam.getPage());
        pageResult.setPagesize(recommendUserQueryParam.getPagesize());

        PageInfo<RecommendUser> pageInfo = this.recommendUserService.queryRecommendUserList(user.getId(), recommendUserQueryParam.getPage(), recommendUserQueryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if (CollectionUtils.isEmpty(records)) {//没有查询到推荐列表
            return pageResult;
        }

        //填充个人信息

        //收集推荐用户的id
        Set<Long> userIds = new HashSet<>();
        for (RecommendUser record : records) {
            userIds.add(record.getUserId());
        }

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);

        //构造查询条件  //因为测试数据比较少,这样查询完之后就没数据了,所以先注释掉条件
//        if (StringUtils.isNotEmpty(recommendUserQueryParam.getCity())) {
//            queryWrapper.eq("city", recommendUserQueryParam.getCity());
//        }
//        if (StringUtils.isNotEmpty(recommendUserQueryParam.getEducation())) {
//            queryWrapper.eq("edu", recommendUserQueryParam.getEducation());
//        }
//        if (StringUtils.isNotEmpty(recommendUserQueryParam.getGender())) {
//            queryWrapper.eq("sex", StringUtils.equals(recommendUserQueryParam.getGender(), "man") ? 1 : 2);
//        }
//        if (null != recommendUserQueryParam.getAge()) {
//            queryWrapper.le("age", recommendUserQueryParam.getAge());
//        }

        List<UserInfo> userInfoList = userInfoService.queryRecommendUserInfoList(queryWrapper);
        if (CollectionUtils.isEmpty(userInfoList)) {
            //没有查询到用户的基本信息
            return pageResult;
        }

        List<TodayBest> todayBests = new ArrayList<>();
//        Set<TodayBest> todayBests = new TreeSet<>((o1, o2) -> (int) (o2.getFateValue() - o1.getFateValue()));
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();

            todayBest.setId(userInfo.getUserId());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setAge(userInfo.getAge());

            //缘分值
            for (RecommendUser record : records) {
                if (record.getUserId().longValue() == userInfo.getUserId().longValue()) {
                    double score = Math.floor(record.getScore());//取整,98.2 -> 98
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }

            todayBests.add(todayBest);
        }

        Collections.sort(todayBests, (o1, o2) -> (int) (o2.getFateValue() - o1.getFateValue()));
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
